home *** CD-ROM | disk | FTP | other *** search
/ Collection of Tools & Utilities / Collection of Tools and Utilities.iso / print / gs261sr1.zip / GDEVPRN.C < prev    next >
C/C++ Source or Header  |  1993-05-26  |  12KB  |  383 lines

  1. /* Copyright (C) 1990, 1992 Aladdin Enterprises.  All rights reserved.
  2.  
  3. This file is part of Ghostscript.
  4.  
  5. Ghostscript is distributed in the hope that it will be useful, but
  6. WITHOUT ANY WARRANTY.  No author or distributor accepts responsibility
  7. to anyone for the consequences of using it or for whether it serves any
  8. particular purpose or works at all, unless he says so in writing.  Refer
  9. to the Ghostscript General Public License for full details.
  10.  
  11. Everyone is granted permission to copy, modify and redistribute
  12. Ghostscript, but only under the conditions described in the Ghostscript
  13. General Public License.  A copy of this license is supposed to have been
  14. given to you along with Ghostscript so you can know your rights and
  15. responsibilities.  It should be in a file named COPYING.  Among other
  16. things, the copyright notice and this notice must be preserved on all
  17. copies.  */
  18.  
  19. /* gdevprn.c */
  20. /* Generic printer support for Ghostscript */
  21. #include "gdevprn.h"
  22. #include "gp.h"
  23. #include "gsprops.h"
  24.  
  25. /* Define the scratch file name prefix for mktemp */
  26. #define SCRATCH_TEMPLATE gp_scratch_file_name_prefix
  27.  
  28. /* Internal routine for opening a scratch file */
  29. private int
  30. open_scratch(char *fname, FILE **pfile)
  31. {    char fmode[4];
  32.     strcpy(fmode, "w+");
  33.     strcat(fmode, gp_fmode_binary_suffix);
  34.     *pfile = gp_open_scratch_file(SCRATCH_TEMPLATE, fname, fmode);
  35.     if ( *pfile == NULL )
  36.        {    eprintf1("Could not open the scratch file %s.\n", fname);
  37.         return_error(gs_error_invalidfileaccess);
  38.        }
  39.     return 0;
  40. }
  41.  
  42. /* ------ Standard device procedures ------ */
  43.  
  44. /* Macros for casting the pdev argument */
  45. #define ppdev ((gx_device_printer *)pdev)
  46. #define pmemdev ((gx_device_memory *)pdev)
  47. #define pcldev ((gx_device_clist *)pdev)
  48.  
  49. gx_device_procs prn_std_procs =
  50.   prn_procs(gdev_prn_open, gdev_prn_output_page, gdev_prn_close);
  51.  
  52. /* Generic initialization for the printer device. */
  53. /* Specific devices may wish to extend this. */
  54. int
  55. gdev_prn_open(gx_device *pdev)
  56. {    const gx_device_memory *mdev =
  57.       gdev_mem_device_for_bits(pdev->color_info.depth);
  58.     gx_device_procs *pprocs = pdev->procs;
  59.     ulong mem_space;
  60.     byte *base = 0;
  61.     char *left = 0;
  62.     if ( mdev == 0 )
  63.         return_error(gs_error_rangecheck);
  64.     memset(ppdev->skip, 0, sizeof(ppdev->skip));
  65.     ppdev->orig_procs = pprocs;
  66.     /*
  67.      * We should initialize the page count to 0, but since resetting
  68.      * some properties like page size closes and reopens the driver,
  69.      * we just have to rely on the linker initializing uninitialized
  70.      * fields to 0.  When we make page_count a property of all devices
  71.      * (which is a big deal, because many drivers currently initialize
  72.      * their structures statically and therefore can't handle changes
  73.      * in the structure), this problem will go away.  Meanwhile:
  74.      */
  75.         /* ppdev->page_count = 0; */
  76.     ppdev->file = ppdev->ccfile = ppdev->cbfile = NULL;
  77.     mem_space = gdev_mem_bitmap_size(pmemdev);
  78.     if ( mem_space >= ppdev->max_bitmap ||
  79.          mem_space != (uint)mem_space ||    /* too big to allocate */
  80.          (base = (byte *)gs_malloc((uint)mem_space, 1, "printer buffer(open)")) == 0 ||    /* can't allocate */
  81.          (PRN_MIN_MEMORY_LEFT != 0 &&
  82.           (left = gs_malloc(PRN_MIN_MEMORY_LEFT, 1, "printer memory left")) == 0)    /* not enough left */
  83.        )
  84.     {    /* Buffer the image in a command list. */
  85.         uint space;
  86.         int code;
  87.         /* Release the buffer if we allocated it. */
  88.         if ( base != 0 )
  89.             gs_free((char *)base, (uint)mem_space, 1,
  90.                 "printer buffer(open)");
  91.         for ( space = ppdev->use_buffer_space; ; )
  92.         {    base = (byte *)gs_malloc(space, 1,
  93.                          "command list buffer(open)");
  94.             if ( base != 0 ) break;
  95.             if ( (space >>= 1) < PRN_MIN_BUFFER_SPACE )
  96.                 return_error(gs_error_VMerror);    /* no hope */
  97.         }
  98. open_c:        pcldev->data = base;
  99.         pcldev->data_size = space;
  100.         pcldev->target = pdev;
  101.         pcldev->mdev = *mdev;
  102.         pcldev->mdev.target = pdev;
  103.         ppdev->buf = base;
  104.         ppdev->buffer_space = space;
  105.         /* Try opening the command list, to see if we allocated */
  106.         /* enough buffer space. */
  107.         code = (*gs_clist_device_procs.open_device)((gx_device *)pcldev);
  108.         if ( code < 0 )
  109.         {    /* If there wasn't enough room, and we haven't */
  110.             /* already shrunk the buffer, try enlarging it. */
  111.             if ( code == gs_error_limitcheck &&
  112.                  space >= ppdev->use_buffer_space
  113.                )
  114.             {    gs_free((char *)base, space, 1,
  115.                     "command list buffer(retry open)");
  116.                 space <<= 1;
  117.                 base = (byte *)gs_malloc(space, 1,
  118.                          "command list buffer(retry open)");
  119.                 ppdev->buf = base;
  120.                 if ( base != 0 ) goto open_c;
  121.             }
  122.             /* Fall through with code < 0 */
  123.         }
  124.         if ( code < 0 ||
  125.              (code = open_scratch(ppdev->ccfname, &ppdev->ccfile)) < 0 ||
  126.              (code = open_scratch(ppdev->cbfname, &ppdev->cbfile)) < 0
  127.            )
  128.         {    /* Clean up before exiting */
  129.             gdev_prn_close(pdev);
  130.             return code;
  131.         }
  132.         pcldev->cfile = ppdev->ccfile;
  133.         pcldev->bfile = ppdev->cbfile;
  134.         pcldev->bfile_end_pos = 0;
  135.         ppdev->mod_procs = gs_clist_device_procs;
  136.     }
  137.     else
  138.     {    /* Render entirely in memory. */
  139.         /* Release the leftover memory. */
  140.         gs_free(left, PRN_MIN_MEMORY_LEFT, 1,
  141.             "printer memory left");
  142.         ppdev->buffer_space = 0;
  143.         pmemdev->base = base;
  144.         ppdev->mod_procs = *mdev->procs;
  145.     }
  146.     /* Synthesize the procedure vector. */
  147.     /* Rendering operations come from the memory or clist device, */
  148.     /* non-rendering come from the printer device. */
  149.     pdev->procs = &ppdev->mod_procs;
  150. #define copy_proc(p) ppdev->mod_procs.p = pprocs->p
  151.     copy_proc(get_initial_matrix);
  152.     copy_proc(output_page);
  153.     copy_proc(close_device);
  154.     copy_proc(map_rgb_color);
  155.     copy_proc(map_color_rgb);
  156.     copy_proc(get_props);
  157.     copy_proc(put_props);
  158.     copy_proc(map_cmyk_color);
  159.     copy_proc(get_xfont_procs);
  160.     copy_proc(get_xfont_device);
  161. #undef copy_proc
  162.     return (*pdev->procs->open_device)(pdev);
  163. }
  164.  
  165. /* Added properties for printers */
  166.  
  167. private const gs_prop_item props_prn[] = {
  168.         /* Read-write properties. */
  169.     prop_def("BufferSpace", prt_int),
  170.     prop_def("MaxBitmap", prt_int),
  171.     prop_def("OutputFile", prt_string),
  172.         /* Read-only properties. */
  173.     prop_def("PageCount", prt_int)
  174. };
  175.  
  176. /* Get properties.  In addition to the standard properties, */
  177. /* we supply the max bitmap size, buffer size, and output name. */
  178. int
  179. gdev_prn_get_props(gx_device *pdev, gs_prop_item *plist)
  180. {    int start = gx_default_get_props(pdev, plist);
  181.     if ( plist != 0 )
  182.        {    register gs_prop_item *pi = plist + start;
  183.         memcpy(pi, props_prn, sizeof(props_prn));
  184.         pi[0].value.i = ppdev->use_buffer_space;
  185.         pi[1].value.i = ppdev->max_bitmap;
  186.         pi[2].value.a.p.s = ppdev->fname;
  187.         pi[2].value.a.size = -1;
  188.         pi[3].value.i = ppdev->page_count;
  189.        }
  190.     return start + sizeof(props_prn) / sizeof(gs_prop_item);
  191. }
  192.  
  193. /* Put properties. */
  194. /* Note that setting the buffer sizes closes the device. */
  195. int
  196. gdev_prn_put_props(gx_device *pdev, gs_prop_item *plist, int count)
  197. {    gs_prop_item *known[3];
  198.     int code = 0;
  199.     props_extract(plist, count, props_prn, 3, known, 0);
  200.     code = gx_default_put_props(pdev, plist, count);
  201.     if ( code < 0 ) return code;
  202.     if ( known[0] != 0 )
  203.        {    gs_prop_item *pi = known[0];
  204.         if ( pi->value.i < 10000 )
  205.             pi->status = pv_rangecheck,
  206.             code = gs_error_rangecheck;
  207.         else
  208.         {    ppdev->use_buffer_space = known[0]->value.i;
  209.             if ( code == 0 ) code = 1;
  210.         }
  211.        }
  212.     if ( known[1] != 0 )
  213.         ppdev->max_bitmap = known[1]->value.i;
  214.     if ( known[2] != 0 )
  215.        {    gs_prop_item *pn = known[2];
  216.         int size = pn->value.a.size;
  217.         if ( size >= prn_fname_sizeof )
  218.             pn->status = pv_limitcheck,
  219.             code = gs_error_limitcheck;
  220.         else
  221.            {    /* Close the file if it's open. */
  222.             if ( ppdev->file != NULL && ppdev->file != stdout )
  223.                 gp_close_printer(ppdev->file, ppdev->fname);
  224.             ppdev->file = NULL;
  225.             memcpy(ppdev->fname, pn->value.a.p.s, size);
  226.             ppdev->fname[size] = 0;
  227.             if ( code == 0 ) code = 1;
  228.            }
  229.        }
  230.     if ( code < 0 )
  231.         return_error(code);
  232.     /* If we're changing the buffer sizes, close the device; */
  233.     /* gs_putdeviceprops will reopen it. */
  234.     if ( pdev->is_open && code )
  235.     {    int ccode = gs_closedevice(pdev);
  236.         if ( ccode < 0 ) return ccode;
  237.     }
  238.     retur